<?php

class ProductController extends Controller
{
   /**
    * Set layout
   */
   public $layout = '//layouts/role';
   
   /**
	 * action array filter
	 */
   public function filters()
	{
		return array(
         'accessControl', // perform access control for CRUD operations
      );
	}
   
   /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
   public function accessRules(){
      $rights = Role::getRights($this->id);
      return $rights;
   }
   
   /**
	 * Check login all action
	 */
   public function beforeAction($action){
      if(is_null(Yii::app()->user->id))
         $this->redirect('/admin/default/login');
      else
         return true;
   }
	public function actionIndex(){
	  
     
      $page = Yii::app()->request->getParam('page',1); // Lấy số trang
      $limit = Yii::app()->request->getParam('limit',10); // Lấy số dòng hiển thị trên grid
      $start = $limit*$page - $limit; // Thứ tự bản ghi bắt đầu lấy  
      $list = array();
      $model = new Product();  
      $cate = new Category();
      $all_cate = $cate->getAll();
      $total_record = $model->count();
      if(!is_numeric($page)) $page = 1;
      if(!is_numeric($limit)) $limit = 10;
      if( $total_record >0 ) {
      	$total_pages = ceil($total_record/$limit);
      } else {
      	$total_pages = 0;
      }
      
      $object = $model->findAll(array('offset' => $start,
                                       'limit' => $limit));
      if(count($object) > 0){
         foreach($object as $item){
            $list[$item['pro_id']] = $item->attributes;
         }
      }
      //CVarDumper::dump($all_cate,10,true);die();
		$this->render('index',array('list' => $list,
                                  'all_cate' => $all_cate,
                                  'limit' => $limit,
                                  'start' => $start,
                                  'page' => $page,
                                  'total_pages' => $total_pages,
                                  'total_record' => $total_record));  
	}
   
   public function actionCreate(){
      
      $cate = new Category();
      $image = new Image();
      $message = '';
      $all_cate = $cate->getAll();
      $tree_cate = $cate->getTree($all_cate);
      
      $model = new Product();
      if(isset($_POST['Product'])){
         $model->attributes = $_POST['Product'];
         if($model->validate()){
            $model->save();
            $message = 'Thêm mới thành công';
         }
         //CVarDumper::dump($model->attributes,10,true);die();
      }
      $this->layout = '//layouts/forgen';
      $this->render('create',array('model' => $model,
                                    'tree_cate' => $tree_cate,
                                    'message' => $message));
      
   }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}